package com.example.asus.diary_demo.RecyclerView;

import android.content.Context;
import android.database.Cursor;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.CursorAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.asus.diary_demo.Diary;
import com.example.asus.diary_demo.R;

import java.text.SimpleDateFormat;
import java.util.List;

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder>implements View.OnClickListener,View.OnLongClickListener{
    private List<Diary> mDiaryTextList;

    private OnMyItemClickListener listener;

    //private final View.OnClickListener mOnClickListener=new OnMyItemClickListener();

    // Because RecyclerView.Adapter in its current form doesn't natively
    // support cursors, we wrap a CursorAdapter that will do all the job
    // for us.
//    private CursorAdapter mCursor;
//
//    private Context mContext;
//
//    public RecyclerViewAdapter(Context context, Cursor c) {
//
//        mContext = context;
//
//        mCursor = new CursorAdapter(mContext, c, 0) {
//
//
//            @Override
//            public View newView(Context context, Cursor cursor, ViewGroup parent) {
//                // Inflate the view here
//                return null;
//            }
//
//            @Override
//            public void bindView(View view, Context context, Cursor cursor) {
//                // Binding operations
//                TextView title=(TextView)view.findViewById(R.id.text1);
//                TextView date=(TextView)view.findViewById(R.id.created);
//            }
//        };
//    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        //View diaryView;
        TextView title, date;

        public ViewHolder(View view){
            super(view);
            //diaryView=view;
            title=(TextView)view.findViewById(R.id.text1);
            date=(TextView)view.findViewById(R.id.created);
        }
    }
    public RecyclerViewAdapter(List<Diary>DiaryTextList){
        mDiaryTextList=DiaryTextList;
    }

    public ViewHolder onCreateViewHolder(ViewGroup parent,int viewType){
        View view= LayoutInflater.from(parent.getContext())
                .inflate(R.layout.diary_row,parent,false);
        ViewHolder holder=new ViewHolder(view);

        holder.itemView.setOnClickListener(this);
        holder.itemView.setOnLongClickListener(this);

        return holder;
    }


    public void onBindViewHolder(ViewHolder holder, final int position){
        Diary diary=mDiaryTextList.get(position);
        holder.title.setText(diary.getTitle());
        holder.date.setText(new SimpleDateFormat("yyyy/MM/dd    HH:mm:ss").format(diary.getDate()));
        holder.itemView.setTag(position);

        ViewHolder itemViewHolder = (ViewHolder) holder;
        ViewGroup.LayoutParams layoutParams = itemViewHolder.itemView.getLayoutParams();
        layoutParams.height = LinearLayout.LayoutParams.WRAP_CONTENT;
    }

    public int getItemCount(){
        return mDiaryTextList.size();
    }

    public void setOnMyItemClickListener(OnMyItemClickListener listener){
        this.listener = listener;
    }
    public interface OnMyItemClickListener{
        void myClick(View v,int position);
        boolean mLongClick(View v,int position);
    }

    @Override
    public void onClick(View v) {
        if(listener!=null){
            listener.myClick(v,(Integer)v.getTag());
        }
    }

    @Override
    public boolean onLongClick(View v) {
        return listener!=null&&listener.mLongClick(v,(Integer)v.getTag());
    }
}
